package com.tianyadu.mmall.core.validate.code.service.impl;

import com.tianyadu.mmall.core.properties.MallSecurityProperties;
import com.tianyadu.mmall.core.validate.code.ValidateCode;
import com.tianyadu.mmall.core.validate.code.service.ValidateCodeGenerator;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * 短信验证码生成
 *
 * @author xueyj
 * @date 2019-11-13 22:39
 */
@Component
public class SmsValidateCodeGenerator implements ValidateCodeGenerator {

    /**
     * 获取Security配置属性
     */
    @Autowired
    private MallSecurityProperties securityProperties;

    /**
     * 生成校验码
     *
     * @param request ServletWebRequest
     * @return com.tianyadu.mmall.core.validate.code.image.ImageCode
     * @author xueyj
     * @date 2019/11/12-21:45
     */
    @Override
    public ValidateCode generate(ServletWebRequest request) {
        //  验证码长度
        int length = securityProperties.getCode().getSms().getLength();
        //  验证码过期时间
        int expireIn = securityProperties.getCode().getSms().getExpireIn();
        //  生成指定长度随机验证
        String code = RandomStringUtils.randomNumeric(length);
        return new ValidateCode(code, expireIn);
    }
}